package com.yangqi.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class OutputTxt {
    public static void main(String[] args) {
//        将以下内容输出到文件 D:\home\cache\shi\shi.txt
        String[] strArr = {"静夜思","【作者】李白 【朝代】唐译文对照","床前明月光，疑是地上霜。","","举头望明月，低头思故乡。"};
        String ss = "静夜思\n" +
                "【作者】李白 【朝代】唐译文对照\n" +
                "床前明月光，疑是地上霜。\n" +
                "\n" +
                "举头望明月，低头思故乡。";
        String path = "D:\\home\\cache\\shi\\shi.txt";
        File file = new File(path);
        File dir = file.getParentFile();
        if(!dir.exists()){
            dir.mkdirs();
        }
        if(!file.exists()){
            try {
                file.createNewFile();
            }catch (IOException e){
                throw new RuntimeException(e);
            }
        }
        BufferedWriter bw = null;
       try{
           bw = new BufferedWriter(new FileWriter(path));
//           for (String item : strArr) {
//               System.out.println(item);
//               bw.write(item);
//               bw.newLine();
//           }
           bw.write(ss);
       }catch(IOException e){
           throw new RuntimeException(e);
       }finally {
           if(bw != null){
               try {
                   bw.close();
               } catch (IOException e) {
                   throw new RuntimeException(e);
               }
           }
       }


    }
}
